package com.txzt.gaea.provide.controller;

import com.txzt.base.core.RtnInfo;
import com.txzt.gaea.api.client.AdminRemoteService;
import com.txzt.gaea.api.client.DeptRemoteService;
import com.txzt.gaea.dto.DeptDTO;
import com.txzt.gaea.provide.service.biz.DeptApiService;
import com.txzt.gaea.provide.utils.LogUtils;
import com.txzt.gaea.vo.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 文件服务器对接gaea门户部分用户，菜单接口
 */
@Validated
@RestController
@RequestMapping("/dept")
public class DeptController {

    @Value("${tool.code}")
    String toolCode;
    @Value("${tool.version}")
    String toolVersion;
    @Value("${deptConfig.rootDeptId}")
    Long rootDeptId;

    @Autowired
    private AdminRemoteService adminRemoteService;
    @Autowired
    private DeptRemoteService deptRemoteService;
    @Autowired
    private DeptApiService deptApiService;


    @RequestMapping({"/getDeptTree"})
    RtnInfo<List<ZTreeNodeVO>> getDeptTree(@RequestBody DeptDTO var1) {
        LogUtils.logSave("dept#getDeptTree", "查询菜单", "com.txzt.gaea.provide.controller.DeptController");
        RtnInfo<List<ZTreeNodeVO>> result = adminRemoteService.getDeptTree(var1);
        if (result == null) {
            return RtnInfo.SUCCESS;
        }
        List<ZTreeNodeVO> list = result.getData();
        if (CollectionUtils.isEmpty(list)) {
            return RtnInfo.SUCCESS;
        }
        for (ZTreeNodeVO z : list) {
            if (StringUtils.equals(z.getId(), "0")) {
                z.setpId(null);
            }
        }
        return result;
    }

    @RequestMapping({"/deptTree/list"})
    public RtnInfo deptTreeList(@RequestBody DeptDTO var1) {
        if (null == var1.getParentNodeId()){
            var1.setParentNodeId(rootDeptId);
        }
        RtnInfo<List<ZTreeNodeVO>> result = adminRemoteService.getDeptTree(var1);
        if (result == null) {
            return RtnInfo.SUCCESS;
        }
        List<ZTreeNodeVO> list = result.getData();
        if (CollectionUtils.isEmpty(list)) {
            return RtnInfo.SUCCESS;
        }
        List<Map<String,Object>> deptList = new ArrayList<>();
        for (ZTreeNodeVO z : list) {
            if (StringUtils.equals(z.getId(), "0")) {
                continue;
            }
            Map<String,Object> first = new HashMap<>();
            first.put("id",Long.parseLong(z.getId()));
            first.put("label",z.getName());
            first.put("value",Long.parseLong(z.getId()));
            first.put("isParent",z.getIsParent());
            if (z.getIsParent()) {
                first.put("children",getChildNode(first));
            }
            deptList.add(first);
        }
        return RtnInfo.success(deptList);
    }

    private List<Map<String,Object>> getChildNode(Map<String,Object> node){
        List<Map<String,Object>> childList = new ArrayList<>();
        if ((boolean)node.get("isParent")){
            //查询子集
            DeptDTO var1 = new DeptDTO();
            var1.setToolCode(toolCode);
            var1.setToolVersion(toolVersion);
            var1.setParentNodeId((Long) node.get("id"));
            RtnInfo<List<ZTreeNodeVO>> result = adminRemoteService.getDeptTree(var1);
            if (result == null) {
                return childList;
            }
            List<ZTreeNodeVO> list = result.getData();
            if (CollectionUtils.isEmpty(list)) {
                return childList;
            }
            for (ZTreeNodeVO z : list){
                Map<String,Object> map = new HashMap<>();
                map.put("id",Long.parseLong(z.getId()));
                map.put("label",z.getName());
                map.put("value",Long.parseLong(z.getId()));
                map.put("isParent",z.getIsParent());
                childList.add(map);
            }
        }
        for (Map<String,Object> item : childList) {
            if ((boolean)item.get("isParent")){
                item.put("children",getChildNode(item));
            }
        }
        if(childList.size() == 0){
            return new ArrayList<>();
        }
        return childList;
    }

    @RequestMapping({"/getDeptList"})
    public RtnInfo<List<DeptVO>> getDeptList(@RequestBody DeptDTO var1){
        var1.setToolCode(toolCode);
        var1.setToolVersion(toolVersion);
//        var1.setParentNodeId((Long) node.get("id"));
        RtnInfo<List<DeptVO>> deptList = deptRemoteService.queryDept(var1);
//        return RtnInfo.success(deptApiService.deptInfoList());
        return deptList;
    }
}
